<?php 
class ControllerAccountOpenIdAuth extends Controller {
	private $error = array();
	
	public function index() {
		$provider = isset($this->request->get['provider'])?$this->request->get['provider']:'';
		
		$openid = new LightOpenID("maam.vn");
	
		if(!$openid->mode) {
			$ui = "";
			
			if($provider == 'yahoo'){
				$openid->identity = 'https://me.yahoo.com';
			} else if ($provider == 'google'){
				$ui = "&openid.ns.ui=" . urlencode('http://specs.openid.net/extensions/ui/1.0') . "&openid.ui.mode=popup";
				$openid->identity = 'https://www.google.com/accounts/o8/id'; 
			}
			
			$openid->required = array('namePerson', 'namePerson/first', 'namePerson/last', 'contact/email' );
			$this->redirect($openid->authUrl() . $ui);
		} elseif($openid->mode == 'cancel') {
			$this->data['cancel'] = '1';
		} else {
			// User aggree to login with his account and the login server sent user's information back
			$attributes = $openid->getAttributes();
			
			// load models
			$this->load->model('account/customer');
			
			unset($this->session->data['guest']);
			
			$user = $this->model_account_customer->getCustomerByEmail( $attributes['contact/email'] );
			
			$exist_user = false;
			if(!empty($user) && isset($user['email']) && $user['email'] != '' && isset($user['customer_id']) )
			{
				$exist_user 	= true;
			}
			
			
			if($exist_user)
			{
				// Login for this user to system
				$this->customer->loginOpenID($attributes['contact/email']);
				
				// Added strpos check to pass McAfee PCI compliance test (http://forum.opencart.com/viewtopic.php?f=10&t=12043&p=151494#p151295)
				if (isset($this->request->get['redirect']) && (strpos($this->request->get['redirect'], HTTP_SERVER) !== false || strpos($this->request->get['redirect'], HTTPS_SERVER) !== false)) {
					$this->data['redirect'] = str_replace('&amp;', '&', $this->request->get['redirect']);
				} else {
					$this->data['redirect'] =  $this->url->link('account/account', '', 'SSL');
				}
			} else {
				$user_data = array();
				$user_data['password'] 		= rand(1, 1000000000);
				
				if(isset($attributes['namePerson/first'])) {
					$user_data['firstname'] 	= $attributes['namePerson/first'];
					$user_data['lastname'] 		= $attributes['namePerson/last'];
				} else {
					$arr_name = explode(" ", $attributes['namePerson']);
					if(sizeof($arr_name) > 0) {
						$user_data['firstname'] = $arr_name[0];
						$user_data['lastname'] = trim(str_replace($arr_name[0], "", $attributes['namePerson']));
					}
				}
				
				$user_data['email'] 		= $attributes['contact/email'];
				$user_data['telephone'] 	= '';
				$user_data['fax'] 			= '';
				$user_data['newsletter'] 	= 0;
				$user_data['customer_group_id'] = (int)$this->config->get('config_customer_group_id');
				$user_data['status'] 		= 1;
				$user_data['approved'] 		= 1;
				
/* 				print_r($attributes);
				echo "============";
				print_r($user_data);
 */				
				$user_id = (int)$this->model_account_customer->addCustomerByOpenID( $user_data );
				
				// Login for this user to system
				$this->customer->loginOpenID($attributes['contact/email']);
				
				$this->data['redirect'] =  $this->url->link('account/account', '', 'SSL');
			}
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/open_id_auth.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/open_id_auth.tpl';
		} else {
			$this->template = 'default/template/account/open_id_auth.tpl';
		}

		$this->response->setOutput($this->render());
  	}
}
?>